<?php
namespace Chat\Controller;
 
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use Zend\Form\Annotation\AnnotationBuilder;
use TestAjax\Model\TestEntity;
use Zend\EventManager\EventManager;
use Zend\EventManager\Event;
 
class ChatController extends AbstractActionController
{
	private $chatService;
	private $chatMessageMapper;
	protected $events;
	
// 	@param $chatService

	public function setChatService($service){
		$this->chatService = $service;
	}
	
	public function getChatService(){
		return $this->$chatService;
	}
	
	public function setChatMessageMapper($Mapper){
		$this->chatMessageMapper =  $Mapper;
	}
	
	public function getChatMessageMapper(){
		return $this->chatMessageMapper;
	}
	
	public function updateUserListAction(){
		$this->layout()->setVariable('userList',$this->getChatService()->getAllUsersButMe());
	}
	
       public function updateChatAction(){
       	
       		//$result = new JsonModel(array(
       	//		'users' => array("1" =>"Hallo@David")
       	//));
       }
     
    protected function getForm()
    {
        $builder    = new AnnotationBuilder();
        $entity     = new TestEntity();
        $form       = $builder->createForm($entity);
         
        return $form;
    }
     
    public function events()
    {
    	if (!$this->events) {
    		$this->events = new EventManager(__CLASS__);
    	}
    
    	return $this->events;
    }

    public function indexAction(){
    	return new ViewModel();
    }
        
    public function sendAction()
    {
    	
        $request = $this->getRequest();
        $response = $this->getResponse();
         
        //$this->getView()->headScript()->appendScript("jQuery(function(){alert('eter');});");
        
     
        if ($request->isPost()){
        	$post_data = $request->getPost();
        	$post_txtChat =$post_data['textChat'];
        	$post_senderID =$post_data['senderID'];
        	$post_receiverID =$post_data['receiverID'];
        	
        	
        	
        	$this->chatService->sendChatMessage($post_txtChat,$post_senderID,$post_receiverID);
        	
        	$this->events()->attachAggregate($this->chatService);
        	$this->events->trigger('newChatMessage',$this,array('receiverID' => $post_receiverID));
        	\FB::info('event');
        }
         
       
        }
        
        public function retrieveAction()
        {
        	 
        	$request = $this->getRequest();
        	$response = $this->getResponse();
        	 
        	//$this->getView()->headScript()->appendScript("jQuery(function(){alert('eter');});");
        	//$this->_helper->layout->disableLayout();
        	//$this->_helper->viewRenderer->setNoRender(TRUE);
        	 
        	if ($request->isPost()){
        		$post_data = $request->getPost();
        		$post_userID =$post_data['userID'];
        		$post_myID =$post_data['myID'];
        		
        		If ($post_data['msgID']){
        			
        		}else{
        			$messagesByUserID = $this->chatService->retrieveAllChatMessages($post_userID,$post_myID);
        		}
        		
        		
        		
        		 if($messagesByUserID){
        		 	$response->setContent(\Zend\Json\Json::encode($messagesByUserID));
        		 }else{
        		 	$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
        		 }
        	
        		
        	}
        	 
        	return $response;
        }
        
        public function retrieveNewMsgAction()
        {
        
        	$request = $this->getRequest();
        	$response = $this->getResponse();
        
        	//$this->getView()->headScript()->appendScript("jQuery(function(){alert('eter');});");
        	//$this->_helper->layout->disableLayout();
        	//$this->_helper->viewRenderer->setNoRender(TRUE);
        
        	if ($request->isPost()){
        		$post_data = $request->getPost();
        		
        		$post_senderID =$post_data['senderID'];
        		$post_receiverID =$post_data['receiverID'];
        		$post_lastMsgID =$post_data['lastMsgID'];
        
        		
        			$messagesByUserID = $this->chatService->retrieveNewChatMessage($post_senderID,$post_receiverID,$post_lastMsgID);
        		
        
        
        
        		if($messagesByUserID == true){
        			
        			$response->setContent(\Zend\Json\Json::encode($messagesByUserID));
        		}else{
        			$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
        		} 
        
        	}
        
        	return $response;
        }
         public function lastMessageIDFromUserAction(){

         	$request = $this->getRequest();
         	$response = $this->getResponse();
         	
         	if ($request->isPost()){
         		$post_data = $request->getPost();
         		$post_senderID =$post_data['senderID'];
         		$post_receiverID =$post_data['receiverID'];
         		
         		$messagesByUserID = $this->chatService->lastChatMessageFromUser($post_senderID,$post_receiverID);
         	
         		if($messagesByUserID){
         			$response->setContent(\Zend\Json\Json::encode($messagesByUserID));
         		}else{
         			$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
         		}
         	
         	}
         	return $response;
         }
        
    }
    

